local skels = {}

local beidoushenquan = fk.CreateSkill{
    name = "ym2__beidoushenquan"
}
Fk:loadTranslationTable{
    ["ym2__beidoushenquan"] = "北斗神拳",
    [":ym2__beidoushenquan"] = "出牌阶段限一次，你可以视为使用目标不公开、依次询问所有角色至一轮无响应的【决斗】，你每响应一次，此【决斗】伤害便+1。",
    ["#ym2__beidoushenquan"] = "北斗神拳：视为使用一张隐藏目标的【决斗】",
}

beidoushenquan:addEffect("viewas", {
    max_phase_use_time = 1,
    anim_type = "offensive",
    prompt = "#ym2__beidoushenquan",
    card_filter = Util.FalseFunc,
    view_as = function(self, player, cards)
        local card = Fk:cloneCard("duel")
        card.skillName = beidoushenquan.name
        return card
    end,
    before_use = function(self, player, use)
        local room = player.room
        use.noIndicate = true
        room:doIndicate(player, room:getOtherPlayers(player))
    end,
    no_indicate = true,
    enabled_at_play = function (self, player)
        return player.phase == Player.Play and self:withinTimesLimit(player, Player.HistoryPhase)
    end,
    enabled_at_response = Util.FalseFunc,
})
beidoushenquan:addEffect(fk.PreCardEffect, {
    can_refresh = function(self, event, target, player, data)
        return data.from == player and data.card.skillName == beidoushenquan.name
    end,
    on_refresh = function(self, event, target, player, data)
        data:changeCardSkill("ym2_beidoushenquan_duel_skill")
    end,
})
beidoushenquan:addAI(nil, "vs_skill")
table.insert(skels, beidoushenquan)

local duel_skill = fk.CreateSkill{
    name = "ym2_beidoushenquan_duel_skill"
}
duel_skill:addEffect("cardskill", {
    prompt = "#duel_skill",
    mod_target_filter = function(self, player, to_select, selected, card)
        return to_select ~= player
    end,
    target_filter = Util.CardTargetFilter,
    target_num = 1,
    mute = true,
    on_effect = function(self, room, effect)
        local to = effect.to
        local from = effect.from
        local responsers = table.simpleClone(room:getOtherPlayers(from))
        table.insertIfNeed(responsers, from)
        local currentTurn = 1
        local damageNum = 1
        local currentResponser = responsers[1]
        local cancelResponser = 0
        local damageCurrent

        while true do
            local respond
            if currentResponser:isAlive() then
                local loopTimes = effect:getResponseTimes(currentResponser)

                for i = 1, loopTimes do
                    local params = { ---@type AskToUseCardParams
                        skill_name = 'slash',
                        pattern = 'slash',
                        cancelable = true,
                        event_data = effect
                    }
                    if loopTimes > 1 then
                        params.prompt = "#AskForResponseMultiCard:::slash:"..i..":"..loopTimes
                    end
                    respond = room:askToResponse(currentResponser, params)
                    if respond then
                        room:responseCard(respond)
                    else
                        break
                    end
                end
            end

            if not respond then
                cancelResponser = cancelResponser + 1
                if not damageCurrent and (currentResponser == to or currentResponser == from) then
                    damageCurrent = currentResponser
                end
            elseif currentResponser == from then
                damageNum = damageNum + 1
            end

            currentTurn = currentTurn % (#responsers) + 1
            currentResponser = responsers[currentTurn]

            if currentTurn == 1 then
                if cancelResponser == #responsers then
                    break
                else
                    cancelResponser = 0
                end
            end
        end

        if damageCurrent and damageCurrent:isAlive() then
            room:damage({
                from = damageCurrent == to and from or to,
                to = damageCurrent,
                card = effect.card,
                damage = damageNum,
                damageType = fk.NormalDamage,
                skillName = duel_skill.name,
            })
        end
    end,
})
table.insert(skels, duel_skill)

return skels